/*--------------------------------------------------------------------------------------*\
**
** adv_keymaster_tent.cpp
**
** Heroes IV
** Copyright 2000, The 3DO Company
**
\*--------------------------------------------------------------------------------------*/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "adv_keymaster_tent.h"

#include "adaptor_handler.h"
#include "adv_object_model.h"
#include "adv_object_type.h"
#include "adventure_frame.h"
#include "adventure_sounds.h"
#include "army.h"
#include "basic_dialog.h"
#include "creature.h"
#include "creature_type.h"
#include "external_string.h"
#include "hero.h"
#include "music.h"
#include "object_registration.h"
#include "player.h"
#include "simple_dialog.h"

static t_register_with_type<t_adv_keymaster_tent> k_registration( k_adv_object_keymaster_tent );

// There is no good way to evaluate the weight of this object.  It's a very important stop, so
// I'm assigning it a large fixed value.
float const k_ai_value_of_keymaster_tent = 3000.0f;

t_adv_keymaster_tent::t_adv_keymaster_tent (  std::string const& source, t_qualified_adv_object_type const& type )
         : t_single_use_object( source )
{
    // Get major subtype (flag color).
    m_guard_color = type.major_subtype();
}

void t_adv_keymaster_tent::activate_trigger( t_army* army, t_adv_map_point const& point, 
		                                    t_direction direction, t_adventure_frame* frame )
{

	t_creature_array&   creatures = army->get_creatures();
    t_player *		    player = army->get_owner();
    std::string         status_text;

	if (player == NULL)
		return;

	t_sound_cache music = get_music_playing();
	if( !player->is_computer() ) 
	{
		stop_music();
		get_dialog_sound( k_dialog_sound_quest )->play( get_sound_volume() );
	}

    // Does the player have the key for this color tent?
	if (player->has_key( get_guard_color() ))
    {
        if (player->is_computer())
			return;

		status_text = get_text ("Empty");
        ok_dialog( status_text, get_dialog_bottom( point ) );

		play_music( music );
        
        return;
    }

    // Give player key.
    player->set_key( get_guard_color() );

	if (player->is_computer())
		return;
    
    /*
    ** Give player key, and show the result.
    */
    t_basic_dialog* result_dialog_ptr;

    result_dialog_ptr = new t_basic_dialog;
    result_dialog_ptr->set_text( get_text ("Initial") );
    result_dialog_ptr->add_ok_button();
    result_dialog_ptr->open( get_dialog_bottom( point ), true, k_align_bottom );
    result_dialog_ptr->run_modal();
	play_music( music );
}

// Maybe text should be "received?"
extern t_external_string const k_text_visited; //( "visited.object" );

std::string t_adv_keymaster_tent::get_balloon_help() const
{
	std::string			result = t_stationary_adventure_object::get_name();
	t_adventure_frame*  adventure_frame = get_adventure_frame();
	t_army*				army = adventure_frame->get_selected_army();
    t_player *		    player_ptr;

	if (army == 0)
		return result;
	player_ptr = army->get_owner();
	if (player_ptr == 0)
		return result;
    // Has the player gotten the key already?
	if (player_ptr->has_key( get_guard_color() ))
	{
		result += " ";
        // Maybe text should be "received?"
		result += k_text_visited;
	}
	return result;
}

void t_adv_keymaster_tent::visit( t_hero* hero )
{
}

float t_adv_keymaster_tent::ai_value( t_adventure_ai const& ai, t_creature_array const& army, int move_cost ) const
{
	if (army.get_owner()->has_key( get_guard_color() ))
		return 0.0f;
	else
		return k_ai_value_of_keymaster_tent;
}

